package com.laoniu.biz.goods;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.laoniu.dto.PageResponse;
import com.laoniu.dto.request.GoodsStockRequestDto;
import com.laoniu.dto.response.GoodsSaleReturnDto;
import com.laoniu.entity.*;
import com.laoniu.service.base.BaseCustomerService;
import com.laoniu.service.base.BaseGoodsService;
import com.laoniu.service.goods.GoodsSaleReturnService;
import com.laoniu.service.goods.GoodsSaleService;
import com.laoniu.utils.SpringSecurityUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
public class GoodsSaleReturnBiz {
    @Autowired
    private GoodsSaleReturnService saleReturnService;
    @Autowired
    private BaseGoodsService goodsService;
    @Autowired
    private BaseCustomerService customerService;
    @Autowired
    private GoodsSaleService saleService;

    public PageResponse<GoodsSaleReturnDto> pageList(GoodsStockRequestDto requestDto) {
        Page<GoodsSaleReturn> page = saleReturnService.listPage(requestDto);
        PageResponse<GoodsSaleReturnDto> result = new PageResponse<>();
        result.setPageNo(requestDto.getPageNo());
        result.setPageSize(requestDto.getPageSize());
        result.setTotal(page.getTotal());
        if (CollectionUtils.isNotEmpty(page.getRecords())){
            Map<Long,String> customerMap = Maps.newHashMap();
            Map<Long,String> goodsMap = Maps.newHashMap();

            List<GoodsSaleReturnDto> dtos = Lists.newArrayList();
            for (GoodsSaleReturn record : page.getRecords()) {
                GoodsSaleReturnDto dto = Convert.convert(GoodsSaleReturnDto.class, record);
                //查询供应商名称
                if (customerMap.containsKey(record.getCustomerId())){
                    dto.setCustomerName(customerMap.get(record.getCustomerId()));
                }else {
                    BaseCustomer customer = customerService.getById(record.getCustomerId());
                    dto.setCustomerName(customer.getName());
                    customerMap.put(customer.getId(), customer.getName());
                }
                //查询商品名称
                if (goodsMap.containsKey(record.getGoodsId())){
                    dto.setGoodsName(goodsMap.get(record.getGoodsId()));
                }else {
                    BaseGoods goods = goodsService.getById(record.getGoodsId());
                    dto.setGoodsName(goods.getName());
                    goodsMap.put(goods.getId(), goods.getName());
                }
                dtos.add(dto);
            }
            result.setData(dtos);
        }
        return result;
    }

    public boolean goodsSaleReturn(GoodsStockRequestDto dto) {
        if (Objects.isNull(dto.getSaleId()) || Objects.isNull(dto.getReturnNum())){
            return false;
        }
        GoodsSale goodsSale = saleService.getById(dto.getSaleId());
        if (Objects.isNull(goodsSale) || goodsSale.getNum() < dto.getReturnNum()){
            return false;
        }
        SecurityUser user = SpringSecurityUtils.getUser();
        GoodsSaleReturn saleReturn = new GoodsSaleReturn();
        saleReturn.setCustomerId(goodsSale.getCustomerId());
        saleReturn.setGoodsId(goodsSale.getGoodsId());
        saleReturn.setPayType(goodsSale.getPayType());
        saleReturn.setReturnTime(LocalDateTime.now());
        saleReturn.setPrice(goodsSale.getPrice());
        saleReturn.setNum(dto.getReturnNum());
        saleReturn.setSumPrice(goodsSale.getPrice().multiply(new BigDecimal(dto.getReturnNum())));
        saleReturn.setRemark(dto.getRemark());
        saleReturn.setOpTime(LocalDateTime.now());
        saleReturn.setOpId(user.getId());
        saleReturn.setOpName(user.getName());
        saleReturnService.save(saleReturn);
        goodsService.addNum(dto.getGoodsId(),dto.getReturnNum());
        return true;
    }
}
